package cn.itcast.usermanage.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.github.abel533.entity.Example.Criteria;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.itcast.usermanage.mapper.NewUserMapper;
import cn.itcast.usermanage.mapper.UserMapper;
import cn.itcast.usermanage.pojo.EasyUIResult;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;

@Service
public class UserServiceImpl implements UserService{
	@Autowired
	private NewUserMapper userMapper;
	
	@Override
	public EasyUIResult queryEasyUIResult(Integer pageNum, Integer pageSize) {
		// 在查询方法调用之前，调用分页插件的静态方法，中间最好不要隔任何代码
				PageHelper.startPage(pageNum, pageSize);
		// 第一个参数是从那条开始，第二个参数是查询多少条
		List<User> list= this.userMapper.select(new User());
		// 初始化pageInfo对象，所有的分页参数都可以在该对象中获取
		PageInfo<User> pageInfo = new PageInfo<>(list);
		EasyUIResult result = new EasyUIResult();
		result.setTotal(pageInfo.getTotal());
		result.setRows(pageInfo.getList());
		return result;
	}
	@Override
	public void addUsers(User user1, User user2) throws Exception {
		this.userMapper.insert(user1);
		// 制造异常
		//int i=1/0;
		this.userMapper.insert(user2);
	}
	@Override
	public Boolean addUser(User user) throws Exception {
		if(user.getId()==null){
			int i = this.userMapper.insert(user);
			if(i>0){
				return true;
			}else{
				return false;
			}
		}else{
			int i =	this.userMapper.updateByPrimaryKeySelective(user);
			if(i>0){
				return true;
			}else{
				return false;
			}
		}
		
	}
	@Override
	public Boolean deleteUser(String[] ids) {
		Example example = new Example(User.class);
		Criteria criteria = example.createCriteria();
		 List<Object> list = Arrays.asList((Object[])ids);
		criteria.andIn("id", list);
		int i = this.userMapper.deleteByExample(example);
		if(i>0){
			return true;
		}else{
			return false;
		}
	}
	

}
